<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class meal extends core {
	private $meal_mod;

	public function __construct() {
		$this->meal_mod = m("meal");
		parent::__construct();
	}

	//列表
	public function index() {
		$where = '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= " AND (m.name LIKE '%{$keyword}%' OR m.description)";
		}
		if (strlen($status)) {
			$where .= " AND m.status='{$status}'";
		}
		//分页
		$this->setpages(array('keyword'=>$keyword,  'status'=>$status));
		$sql = "SELECT m.*, p.name as project_name
			FROM {$this->tbp}meal m LEFT JOIN im_project p ON m.project_id=p.id
			WHERE 1 {$where}
			ORDER BY m.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['status_name'] = $this->meal_mod->status_name($row['status']);
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('page',$nav);
		$this->display();
	}

	//明细
	public function history() {
		$where = '';
		$type = (isset($_GET['type'])) ? trim($_GET['type']) : '';
		$meal_id = (isset($_GET['meal_id'])) ? intval($_GET['meal_id']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (ml.name LIKE '%{$keyword}%' OR ml.description LIKE '%{$keyword}%' OR m.name LIKE '%{$keyword}%')";
		}
		if (strlen($type)) {
			$where .= "AND mh.type='{$type}'";
		}
		if ($meal_id) {
			$where .= "AND mh.meal_id='{$meal_id}'";
		}
		//分页
		$this->setpages(array('keyword'=>$keyword,  'type'=>$type,  'meal_id'=>$meal_id));
		$sql = "SELECT mh.*, m.name as member_name, m.mobile, ml.name as meal_name, ml.price as meal_price,
			o.order_sn
			FROM {$this->tbp}meal_history mh LEFT JOIN im_member m ON mh.member_id=m.id
			LEFT JOIN {$this->tbp}meal ml ON mh.meal_id=ml.id
			LEFT JOIN {$this->tbp}order o ON mh.order_id=o.id
			WHERE 1 {$where}
			ORDER BY mh.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				//$rs[$key]['status_name'] = $this->project_mod->status_name($row['status']);
			}
		}
		$nav = $this->ezr->get_navigation();
		//
		$sql = "SELECT id,name FROM {$this->tbp}meal ORDER BY name ASC, id ASC";
		$meals = $this->db->get_results($sql);
		$this->smarty->assign('meals',$meals);
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('meal_id',$meal_id);
		$this->smarty->assign('type',$type);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);

		$this->display();
	}

	//套餐的购买订单
	public function orders() {
		$where = 'mm.status=1 ';
		$meal_id = (isset($_GET['meal_id'])) ? intval($_GET['meal_id']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (mm.order_sn LIKE '%{$keyword}%' OR ml.name LIKE '%{$keyword}%' OR
					ml.description LIKE '%{$keyword}%' OR m.name LIKE '%{$keyword}%' OR m.mobile LIKE '%{$keyword}%')";
		}
		if ($meal_id) {
			$where .= "AND mm.meal_id='{$meal_id}'";
		}
		//分页
		$this->setpages(array('keyword'=>$keyword,  'meal_id'=>$meal_id));
		$sql = "SELECT mm.*, m.name as member_name, m.mobile, ml.name as meal_name, ml.price as meal_price
			FROM {$this->tbp}member_meal mm LEFT JOIN im_member m ON mm.member_id=m.id
			LEFT JOIN {$this->tbp}meal ml ON mm.meal_id=ml.id
			WHERE {$where}
			ORDER BY mm.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				//$rs[$key]['status_name'] = $this->project_mod->status_name($row['status']);
			}
		}
		$nav = $this->ezr->get_navigation();
		//
		$sql = "SELECT id,name FROM {$this->tbp}meal ORDER BY name ASC, id ASC";
		$meals = $this->db->get_results($sql);
		$this->smarty->assign('meals',$meals);
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('meal_id',$meal_id);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);

		$this->display();
	}

	//为会员增加套餐
	public function manual_meal() {
		if (IS_POST) {
			//$member_id = (isset($_POST['member_id']) && intval($_POST['member_id'])) ? intval($_POST['member_id']) : 0;
			$mobile = (isset($_POST['mobile']) && trim($_POST['mobile'])) ? trim($_POST['mobile']) : '';
			$meal_id = (isset($_POST['meal_id']) && floatval($_POST['meal_id'])) ? floatval($_POST['meal_id']) : 0;
			$num = (isset($_POST['num']) && floatval($_POST['num'])) ? floatval($_POST['num']) : 0;
			if (!$mobile) error("请输入会员手机号码");
			$sql = "SELECT id FROM {$this->tbp}member WHERE mobile='{$mobile}'";
			$member_id = intval($this->db->get_var($sql));
			if ($member_id && $meal_id && $num) {
				$order_sn = "TC".generate_order_sn();
				$sql = "SELECT * FROM {$this->tbp}meal WHERE id='{$meal_id}'";
				$meal = $this->db->get_row($sql);

				$sql = "INSERT INTO {$this->tbp}member_meal(meal_id, member_id, price, num,
					add_time, status, order_sn, pay_method, pay_time)
					VALUES('{$meal_id}', '{$member_id}', '{$meal->price}', '{$num}',
					'".time()."', '1', '{$order_sn}', 'manual', '".time()."')";
				$this->db->query($sql);

				$sql = "INSERT INTO {$this->tbp}meal_history(meal_id, member_id, add_time, type)
					VALUES('{$meal_id}', '{$member_id}', '".time()."', '1')";
				$this->db->query($sql);
			} else {
				error("找不到该手机对应的会员");
			}
			header("Location:?app=meal&act=history");
		} else {
			$member_id = (isset($_GET['member_id']) && intval($_GET['member_id'])) ? intval($_GET['member_id']) : 0;
			//$this->smarty->assign("members", $this->_members());
			$this->smarty->assign("meals", $this->_meals());
			$this->smarty->assign("member_id", $member_id);
			$this->display();
		}
	}

	private function _members() {
		$sql = "SELECT id, name, mobile FROM {$this->tbp}member ORDER BY name ASC";
		$members = $this->db->get_results($sql);
		return $members;
	}

	private function _meals() {
		$sql = "SELECT id, name FROM {$this->tbp}meal ORDER BY id ASC";
		$rs = $this->db->get_results($sql);
		return $rs;
	}

	//add
	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$this->smarty->assign('projects', $this->_projects());
			$this->display();
		}
	}

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$meal_id = (isset($_GET['meal_id']) && intval($_GET['meal_id'])) ? intval($_GET['meal_id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}meal WHERE id='{$meal_id}'";
			$meal = $this->db->get_row($sql);
			$this->smarty->assign('row', $meal);
			$this->smarty->assign('projects', $this->_projects());
			$this->display();
		}
	}

	//保存
	public function save() {
		$meal_id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$project_id = (isset($_POST['project_id']) && intval($_POST['project_id'])) ? intval($_POST['project_id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$price = (isset($_POST['price']) && floatval($_POST['price'])) ? floatval($_POST['price']) : 0;
		$market_price = (isset($_POST['market_price']) && floatval($_POST['market_price'])) ? floatval($_POST['market_price']) : 0;
		$num = (isset($_POST['num']) && intval($_POST['num'])) ? intval($_POST['num']) : 0;
		$description = (isset($_POST['description']) && trim($_POST['description'])) ? trim($_POST['description']) : '';
		$status = (isset($_POST['status']) && trim($_POST['status'])) ? intval($_POST['status']) : 0;

		$sort = (isset($_POST['sort']) && trim($_POST['sort'])) ? intval($_POST['sort']) : 999;
		//图片
		$list_pic = upload_one_file('meal', 'list_pic');
		$detail_pic = upload_one_file('meal', 'detail_pic');
		if (!$list_pic) $list_pic = (isset($_POST['origin_list_pic']) && trim($_POST['origin_list_pic'])) ? trim($_POST['origin_list_pic']) : '';
		if (!$detail_pic) $detail_pic = (isset($_POST['origin_detail_pic']) && trim($_POST['origin_detail_pic'])) ? trim($_POST['origin_detail_pic']) : '';
		if ($meal_id) {
			$sql = "UPDATE {$this->tbp}meal SET name='{$name}', list_pic='{$list_pic}', detail_pic='{$detail_pic}',
				project_id='{$project_id}', price='{$price}', market_price='{$market_price}', description='{$description}',
				num='{$num}', status='{$status}', sort='{$sort}'
				WHERE id='{$meal_id}' ";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO {$this->tbp}meal(name, list_pic, detail_pic, project_id, price,
				description, market_price, status, sort) VALUES(
				'{$name}', '{$list_pic}', '{$detail_pic}', '{$project_id}', '{$price}',
				'{$description}', '{$market_price}', '{$status}', '{$sort}')";
			$this->db->query($sql);
			$meal_id = $this->db->insert_id;
		}
		//echo $sql;
		//exit;
		header("Location:?app=meal&act=index");
	}

	//删除
	public function delete() {
		$meal_id = (isset($_GET['meal_id']) && trim($_GET['meal_id'])) ? intval($_GET['meal_id']) : 0;
		$sql = "DELETE FROM {$this->tbp}meal WHERE id='{$meal_id}'";
		$this->db->query($sql);
		header("Location:?app=meal&act=index");
	}

	//导出
	public function export() {
		$fields = array(
			'id'=>'ID',
			'member_name'=>'会员',
			'mobile'=>'手机号码',
			'meal_name'=>'套餐',
			'buy_order_sn'=>'关联订单号',
			'type'=>'类型',
			'add_time'=>'下单时间',
		);
		$where = (isset($_GET['where']) && trim($_GET['where'])) ? trim($_GET['where']) : '';
		$where = stripcslashes($where);
		$sql = "SELECT mh.*, m.name as member_name, m.mobile, ml.name as meal_name, ml.price as meal_price,
			o.order_sn as buy_order_sn
			FROM {$this->tbp}meal_history mh LEFT JOIN im_member m ON mh.member_id=m.id
			LEFT JOIN {$this->tbp}meal ml ON mh.meal_id=ml.id
			LEFT JOIN {$this->tbp}order o ON mh.order_id=o.id
			WHERE 1 {$where}
			ORDER BY mh.id DESC";
		//echo $sql;
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				if ($row->type==0) {
					$rs[$key]->type = "使用";
				} else {
					$rs[$key]->type = "购买";
				}
				if ($row->add_time) $rs[$key]->add_time = date("Y-m-d H:i:s", $row->add_time);
			}
			export_excel($rs, $fields, "meal");
		}
	}

	//项目
	private function _projects() {
		$sql = "SELECT * FROM {$this->tbp}project ORDER BY sort ASC, id ASC";
		$projects = $this->db->get_results($sql);
		return $projects;
	}
}
